package com.huahua.api.repository;

import com.huahua.api.entity.pojo.WebClipboard;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

/**
 * @author Huahua-Ai
 * @date 2025-01-21 22:18:00
 * @description 网络剪贴板数据访问层
 */
@Repository
public interface WebClipboardRepository extends ReactiveCrudRepository<WebClipboard, Long> {

    /**
     * 根据分享ID查找
     */
    Mono<WebClipboard> findByShareId(String shareId);

    /**
     * 检查分享ID是否存在
     */
    Mono<Boolean> existsByShareId(String shareId);

    /**
     * 删除过期的剪贴板
     */
    @Modifying
    @Query("DELETE FROM web_clipboard WHERE expire_time < :currentTime")
    Mono<Integer> deleteExpiredClipboards(LocalDateTime currentTime);

    /**
     * 查找过期的剪贴板
     */
    @Query("SELECT * FROM web_clipboard WHERE expire_time < :currentTime")
    Flux<WebClipboard> findExpiredClipboards(LocalDateTime currentTime);

    /**
     * 更新访问次数和最后访问时间
     */
    @Modifying
    @Query("UPDATE web_clipboard SET access_count = access_count + 1, last_access_time = :accessTime WHERE share_id = :shareId")
    Mono<Integer> updateAccessInfo(String shareId, LocalDateTime accessTime);

    /**
     * 更新访问密码
     */
    @Modifying
    @Query("UPDATE web_clipboard SET password = :password, updated_time = :updateTime WHERE share_id = :shareId")
    Mono<Integer> updatePassword(String shareId, String password, LocalDateTime updateTime);

    /**
     * 更新管理员密码
     */
    @Modifying
    @Query("UPDATE web_clipboard SET admin_password = :adminPassword, updated_time = :updateTime WHERE share_id = :shareId")
    Mono<Integer> updateAdminPassword(String shareId, String adminPassword, LocalDateTime updateTime);
}